package com.itheima.Controller;

import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.Service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/api/users")
public class UserController {
    //    private static Logger log = LoggerFactory.getLogger(UserControlller.class); 不需要了 使用@Slf4j注解替换
    @Autowired
    private UserService userService;

    //    @RequestMapping(value = "/",method = RequestMethod.GET)  // 设置请求方式为GET
    /*
     *   查询所有用户
     * */
    @GetMapping
    public Result list() {
        log.info("查询所有用户数据");
//        查询用户数据
        List<User> userList = userService.list();
        return Result.success(userList);
    }

    /*
     *   根据id删除用户
     * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id来删除用户:{}", id);
        userService.delete(id);
        return Result.success();
    }

    /*
     *   新增用户
     * */
    @PostMapping
    public Result add(@RequestBody User user) {
        log.info("新增用户,{}", user);
        userService.add(user);
        return Result.success();
    }

    /*
     *   根据id查询用户
     * */
    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id) {
        log.info("根据id来查询用户:{}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    /*
     *   更新用户数据
     * */
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("更新用户数据,{}", user);
        userService.update(user);
        return Result.success();
    }

}
